﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverTile;
using SilverTile.Xaml;
using System.Linq;
using SilverTile.Sprites;

namespace SilverTileApp
{

    public class PlayerSprite : AnimatedSprite
    {

        public const int PLAYER_SPEED = 2;

        public Direction Direction { get; set; }
        public Boolean Moving { get; set; }

        public Boolean Attacking { get; set; }

        public SwordSprite Sword { get;set;}

        public PlayerSprite(int width, int height)
            : base(width, height)
        {
            Direction = Direction.Left;
            Moving = false;
        }

        public void SetDirection(Direction direction)
        {
            if (direction != Direction)
            {
                Direction = direction;
                SetActiveAnimation(direction);
            }
        }

        public void SetActiveAnimation(Direction direction)
        {
            SetActiveAnimation(direction.ToString());
        }

        protected Collision WillCollideIfMoving(Direction direction)
        {

            int dx = 0;
            int dy = 0;
            if (Moving)
            {
                switch (direction)
                {
                    case Direction.Up:
                        dy = -PLAYER_SPEED;
                        break;
                    case Direction.Right:
                        dx = PLAYER_SPEED;
                        break;
                    case Direction.Down:
                        dy = PLAYER_SPEED;
                        break;
                    case Direction.Left:
                        dx = -PLAYER_SPEED;
                        break;
                }
            }

            Collision collides = Viewport.SpriteWillCollideWithBackground(this, X + dx, Y + dy, -1);
            if (X + dx < 0)
            {
                collides = collides.Set(Collision.Left);
            }
            else if (X + dx >= Viewport.Width)
            {
                collides = collides.Set(Collision.Right);
            }

            if (Y + dy < 0)
            {
                collides = collides.Set(Collision.Top);
            }
            else if (Y + dy >= Viewport.Height)
            {
                collides = collides.Set(Collision.Bottom);
            }


            return collides;
        }

        public override void Update(TimeSpan timeSinceLastFrame)
        {

            Collision collision = WillCollideIfMoving(Direction);

            if (!collision.Collides())
            {
                int dx = 0;
                int dy = 0;
                if (Moving)
                {
                    switch (Direction)
                    {
                        case Direction.Up:
                            dy = -PLAYER_SPEED;
                            break;
                        case Direction.Right:
                            dx = PLAYER_SPEED;
                            break;
                        case Direction.Down:
                            dy = PLAYER_SPEED;
                            break;
                        case Direction.Left:
                            dx = -PLAYER_SPEED;
                            break;
                    }


                    X += dx;
                    Y += dy;

                    if (!Sword.Moving)
                    {
                        SetSwordPosition(Sword);
                    }
                    
                    //base.Update(timeSinceLastFrame);
                }
                else
                {
                    // Force the animation to be at frame 0
                    //SetActiveAnimation(ActiveAnimation);
                }
            }
            else if (!Attacking)
            {
                // Force the animation to be at frame 0
                //SetActiveAnimation(ActiveAnimation);                
            }


            base.Update(timeSinceLastFrame);

        }

        public void SetSwordPosition(ISprite sword)
        {
            switch (Direction)
            {
                case SilverTileApp.Direction.Up:
                    sword.X = X + 5;
                    sword.Y = Y - 26;
                    break;
                case SilverTileApp.Direction.Right:
                    sword.X = X + 27;
                    sword.Y = Y + 9;
                    break;
                case SilverTileApp.Direction.Down:
                    sword.X = X + 9;
                    sword.Y = Y + 27;
                    break;
                case SilverTileApp.Direction.Left:
                    sword.X = X - 27;
                    sword.Y = Y + 9;
                    break;
            }
        }

        public void Attack(Action animationCompleteAction)
        {
            if (!Attacking)
            {
                Direction currentDir = Direction;
                Attacking = true;
                SetActiveAnimation(Direction + "_Attack", (a) =>
                {
                    SetActiveAnimation(currentDir);
                    Attacking = false;
                    Sword.IsVisible = false;
                    animationCompleteAction();
                });
            }

        }


    }

}
